//
//  BPPhotographerCommentViewController.m
//  BabyPhotograph
//
//  Created by cesh on 2017/3/17.
//  Copyright © 2017年 cesh. All rights reserved.
//

#import "BPPhotographerCommentViewController.h"
#import "UILabel+BPBoundingRect.h"
#import "BPPreviewViewController.h"
#import <UIImageView+WebCache.h>
@interface BPPhotographerCommentViewController () <UITableViewDelegate, UITableViewDataSource, XLPhotoBrowserDatasource>

@property (strong, nonatomic) UITableView *tableView;
//记录下每个cell的高度
@property (strong, nonatomic) NSMutableArray *cellHeights;
//table view整体的高度
@property (assign, nonatomic) CGFloat tableViewHeight;
//选中的图片
@property (strong, nonatomic) UIImageView *selectedImgV;

@end

@implementation BPPhotographerCommentViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.alpha = 1.0;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.alpha = 1.0;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"摄影师评论 %@", self.data);
    self.title = @"评价";
    self.view.backgroundColor = [UIColor colorWithRed:0xec / 255.0 green:0xf5 / 255.0 blue:0xfc / 255.0 alpha:1.0];
    [self calculateCellHeights];
    [self setupCommentTableView];
}

- (NSMutableArray *)cellHeights {
    if (_cellHeights == nil) {
        _cellHeights = [NSMutableArray array];
    }
    return _cellHeights;
}

//根据label的长度和是否有图片计算出cell的高度
- (CGFloat)heightForCommentData:(NSDictionary *)commentData {
    //获取评论文字
    NSString *commentString = commentData[@"content"];
    //取得文字Lbl的高度
    UILabel *comment =[[UILabel alloc]initWithFrame:CGRectMake(10, 50, screenW - 20, 0)];
    comment.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
    comment.textColor = [UIColor colorWithRed:141/255.0 green:165/255.0 blue:184/255.0 alpha:1/1.0];
    
    comment.text = commentString;
    
    comment.numberOfLines = 0; //PS：这句很主要，否则默认行数为1，只显示一行文字后面截断了就没有了
    CGRect myRect = [comment boundingRectWithInitSize:comment.frame.size];
    
    CGFloat lblHeight = myRect.size.height;
    
    NSArray *imgs = commentData[@"imgs"];
    //判断是否有图片
    NSInteger picHeight = imgs.count == 0 ? 0 : 75;
    
    return lblHeight + 60 + picHeight;
}

- (void)calculateCellHeights {
    CGFloat tableViewHeight = 0;
    for (NSDictionary *commentData in self.data) {
        [self.cellHeights addObject:@([self heightForCommentData:commentData])];
        tableViewHeight += [self heightForCommentData:commentData];
    }
    self.tableViewHeight = tableViewHeight;
}

- (void)setupCommentTableView{
    UITableView *tableView = [[UITableView alloc]init];
    self.tableView = tableView;
    tableView.frame = CGRectMake(0, 0, screenW, screenH);
    tableView.delegate = self;
    tableView.dataSource = self;

    [self.view addSubview:tableView];
}

#pragma mark - table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //    BPCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"commentCell"];
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //头像
    UIImageView *headerImg = [[UIImageView alloc]init];
    headerImg.frame = CGRectMake(10, 10, 30, 30);
    headerImg.layer.cornerRadius = 15.0;
    headerImg.layer.masksToBounds = YES;
    [headerImg sd_setImageWithURL:[NSURL URLWithString:self.data[indexPath.row][@"user"][@"imgs"][0]] placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
    [cell.contentView addSubview:headerImg];
    
    //昵称
    UILabel *name = [[UILabel alloc] init];
    name.origin = CGPointMake(47, 10);
    name.text = self.data[indexPath.row][@"user"][@"nickname"];
    name.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
    name.textColor = [UIColor colorWithRed:141/255.0 green:165/255.0 blue:184/255.0 alpha:1/1.0];
    [name sizeToFit];
    [cell.contentView addSubview:name];
    //日期
    UILabel *date = [[UILabel alloc] init];
    date.origin = CGPointMake(47, 27);
    date.text = self.data[indexPath.row][@"time"];
    date.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    date.textColor = [UIColor colorWithRed:185/255.0 green:198/255.0 blue:208/255.0 alpha:1/1.0];
    [date sizeToFit];
    [cell.contentView addSubview:date];
    //星级
    UIImageView *star1 = [[UIImageView alloc]init];
    UIImageView *star2 = [[UIImageView alloc]init];
    UIImageView *star3 = [[UIImageView alloc]init];
    UIImageView *star4 = [[UIImageView alloc]init];
    UIImageView *star5 = [[UIImageView alloc]init];
    
    star5.frame = CGRectMake(screenW - 11.2 - 14.3, 13, 14.3, 13.6);
    star4.frame = CGRectMake(star5.frame.origin.x - 5 - 14.3, 13, 14.3, 13.6);
    star3.frame = CGRectMake(star4.frame.origin.x - 5 - 14.3, 13, 14.3, 13.6);
    star2.frame = CGRectMake(star3.frame.origin.x - 5 - 14.3, 13, 14.3, 13.6);
    star1.frame = CGRectMake(star2.frame.origin.x - 5 - 14.3, 13, 14.3, 13.6);
    
    NSInteger star = [self.data[indexPath.row][@"star_level"] integerValue];
    switch (star) {
        case 1:
        {
            star1.image = [UIImage imageNamed:@"star_s"];
            star2.image = [UIImage imageNamed:@"star_n"];
            star3.image = [UIImage imageNamed:@"star_n"];
            star4.image = [UIImage imageNamed:@"star_n"];
            star5.image = [UIImage imageNamed:@"star_n"];
        }
            break;
        case 2:
        {
            star1.image = [UIImage imageNamed:@"star_s"];
            star2.image = [UIImage imageNamed:@"star_s"];
            star3.image = [UIImage imageNamed:@"star_n"];
            star4.image = [UIImage imageNamed:@"star_n"];
            star5.image = [UIImage imageNamed:@"star_n"];
        }
            break;
        case 3:
        {
            star1.image = [UIImage imageNamed:@"star_s"];
            star2.image = [UIImage imageNamed:@"star_s"];
            star3.image = [UIImage imageNamed:@"star_s"];
            star4.image = [UIImage imageNamed:@"star_n"];
            star5.image = [UIImage imageNamed:@"star_n"];
        }
            break;
        case 4:
        {
            star1.image = [UIImage imageNamed:@"star_s"];
            star2.image = [UIImage imageNamed:@"star_s"];
            star3.image = [UIImage imageNamed:@"star_s"];
            star4.image = [UIImage imageNamed:@"star_s"];
            star5.image = [UIImage imageNamed:@"star_n"];
        }
            break;
        case 5:
        {
            star1.image = [UIImage imageNamed:@"star_s"];
            star2.image = [UIImage imageNamed:@"star_s"];
            star3.image = [UIImage imageNamed:@"star_s"];
            star4.image = [UIImage imageNamed:@"star_s"];
            star5.image = [UIImage imageNamed:@"star_s"];
        }
            break;
        default:
            break;
    }
    
    [cell.contentView addSubview:star1];
    [cell.contentView addSubview:star2];
    [cell.contentView addSubview:star3];
    [cell.contentView addSubview:star4];
    [cell.contentView addSubview:star5];
    
    UILabel *comment =[[UILabel alloc]initWithFrame:CGRectMake(10, 50, screenW - 20, 0)];
    comment.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
    comment.textColor = [UIColor colorWithRed:141/255.0 green:165/255.0 blue:184/255.0 alpha:1/1.0];
    
    comment.text = self.data[indexPath.row][@"content"];
    
    comment.numberOfLines = 0; //PS：这句很主要，否则默认行数为1，只显示一行文字后面截断了就没有了
    CGRect myRect = [comment boundingRectWithInitSize:comment.frame.size];
    comment.frame = CGRectMake(10, 50, screenW - 20, myRect.size.height);
    
    [cell.contentView addSubview:comment];
    
    //评论图片
    NSArray *imgs = self.data[indexPath.row][@"imgs"];
    if (imgs.count) {
        for (NSInteger i = 0; i < imgs.count; i++) {
            UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(10 + 72 * i, comment.bottom + 10, 66, 66)];
            [imgV sd_setImageWithURL:[NSURL URLWithString:imgs[i]] placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
            [cell.contentView addSubview:imgV];
            imgV.userInteractionEnabled = YES;
            //给图片添加点击放大事件
            [imgV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnCommentImg:)]];
        }
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self.cellHeights[indexPath.row] floatValue];
}

- (void)tapOnCommentImg:(UITapGestureRecognizer *)tap {
    
    UIImageView *selectedImgV = (UIImageView *)tap.view;
    self.selectedImgV = selectedImgV;
    XLPhotoBrowser *browser = [XLPhotoBrowser showPhotoBrowserWithCurrentImageIndex:1 imageCount:1 datasource:self];
    [browser hideChooseBtn];
    browser.browserStyle = XLPhotoBrowserStyleSimple;
    
}

#pragma mark    -   XLPhotoBrowserDatasource

- (UIImage *)photoBrowser:(XLPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    return self.selectedImgV.image;
}

- (UIView *)photoBrowser:(XLPhotoBrowser *)browser sourceImageViewForIndex:(NSInteger)index
{
    return self.selectedImgV;
}
@end
